/*
 * GUI.java
 *
 * Created on 9 de mayo de 2008, 07:44 PM
 */
package presentacion;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import logicaNegocio.Controller;

/**
 *
 * @author  Administrador
 */
public class GUI extends javax.swing.JFrame {

    private int tipoDeDatos = 1;

    /** Creates new form GUI */
    public GUI() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            initComponents();
            this.setLocation(25, 25);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        calculadora = new javax.swing.JPanel();
        argumento = new javax.swing.JTextField();
        boton0 = new javax.swing.JButton();
        boton1 = new javax.swing.JButton();
        boton2 = new javax.swing.JButton();
        boton3 = new javax.swing.JButton();
        boton4 = new javax.swing.JButton();
        boton5 = new javax.swing.JButton();
        boton6 = new javax.swing.JButton();
        boton7 = new javax.swing.JButton();
        boton8 = new javax.swing.JButton();
        boton9 = new javax.swing.JButton();
        botonPunto = new javax.swing.JButton();
        botonSuma = new javax.swing.JButton();
        botonResta = new javax.swing.JButton();
        botonMultiplicacion = new javax.swing.JButton();
        botonDivision = new javax.swing.JButton();
        botonIgual = new javax.swing.JButton();
        botonBorrarDigito = new javax.swing.JButton();
        botonBorrarTodo = new javax.swing.JButton();
        botonSigno = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Calculadora");
        setResizable(false);

        argumento.setFont(new java.awt.Font("Arial", 0, 12));
        argumento.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        boton0.setFont(new java.awt.Font("Arial", 0, 12));
        boton0.setForeground(java.awt.Color.blue);
        boton0.setText("0");
        boton0.setMargin(new java.awt.Insets(2, 2, 2, 2));
        boton0.setName("0"); // NOI18N
        boton0.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarDigito(evt);
            }
        });

        boton1.setFont(new java.awt.Font("Arial", 0, 12));
        boton1.setForeground(java.awt.Color.blue);
        boton1.setText("1");
        boton1.setMargin(new java.awt.Insets(2, 2, 2, 2));
        boton1.setName("1"); // NOI18N
        boton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarDigito(evt);
            }
        });

        boton2.setFont(new java.awt.Font("Arial", 0, 12));
        boton2.setForeground(java.awt.Color.blue);
        boton2.setText("2");
        boton2.setMargin(new java.awt.Insets(2, 2, 2, 2));
        boton2.setName("2"); // NOI18N
        boton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarDigito(evt);
            }
        });

        boton3.setFont(new java.awt.Font("Arial", 0, 12));
        boton3.setForeground(java.awt.Color.blue);
        boton3.setText("3");
        boton3.setMargin(new java.awt.Insets(2, 2, 2, 2));
        boton3.setName("3"); // NOI18N
        boton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarDigito(evt);
            }
        });

        boton4.setFont(new java.awt.Font("Arial", 0, 12));
        boton4.setForeground(java.awt.Color.blue);
        boton4.setText("4");
        boton4.setMargin(new java.awt.Insets(2, 2, 2, 2));
        boton4.setName("4"); // NOI18N
        boton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarDigito(evt);
            }
        });

        boton5.setFont(new java.awt.Font("Arial", 0, 12));
        boton5.setForeground(java.awt.Color.blue);
        boton5.setText("5");
        boton5.setMargin(new java.awt.Insets(2, 2, 2, 2));
        boton5.setName("5"); // NOI18N
        boton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarDigito(evt);
            }
        });

        boton6.setFont(new java.awt.Font("Arial", 0, 12));
        boton6.setForeground(java.awt.Color.blue);
        boton6.setText("6");
        boton6.setMargin(new java.awt.Insets(2, 2, 2, 2));
        boton6.setName("6"); // NOI18N
        boton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarDigito(evt);
            }
        });

        boton7.setFont(new java.awt.Font("Arial", 0, 12));
        boton7.setForeground(java.awt.Color.blue);
        boton7.setText("7");
        boton7.setMargin(new java.awt.Insets(2, 2, 2, 2));
        boton7.setName("7"); // NOI18N
        boton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarDigito(evt);
            }
        });

        boton8.setFont(new java.awt.Font("Arial", 0, 12));
        boton8.setForeground(java.awt.Color.blue);
        boton8.setText("8");
        boton8.setMargin(new java.awt.Insets(2, 2, 2, 2));
        boton8.setName("8"); // NOI18N
        boton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarDigito(evt);
            }
        });

        boton9.setFont(new java.awt.Font("Arial", 0, 12));
        boton9.setForeground(java.awt.Color.blue);
        boton9.setText("9");
        boton9.setMargin(new java.awt.Insets(2, 2, 2, 2));
        boton9.setName("9"); // NOI18N
        boton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarDigito(evt);
            }
        });

        botonPunto.setFont(new java.awt.Font("Arial", 0, 12));
        botonPunto.setForeground(java.awt.Color.magenta);
        botonPunto.setText(".");
        botonPunto.setMargin(new java.awt.Insets(2, 2, 2, 2));
        botonPunto.setName("."); // NOI18N
        botonPunto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarDigito(evt);
            }
        });

        botonSuma.setFont(new java.awt.Font("Arial", 0, 12));
        botonSuma.setForeground(java.awt.Color.red);
        botonSuma.setText("+");
        botonSuma.setMargin(new java.awt.Insets(2, 2, 2, 2));
        botonSuma.setName("1"); // NOI18N
        botonSuma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                operacion(evt);
            }
        });

        botonResta.setFont(new java.awt.Font("Arial", 0, 12));
        botonResta.setForeground(java.awt.Color.red);
        botonResta.setText("-");
        botonResta.setMargin(new java.awt.Insets(2, 2, 2, 2));
        botonResta.setName("2"); // NOI18N
        botonResta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                operacion(evt);
            }
        });

        botonMultiplicacion.setFont(new java.awt.Font("Arial", 0, 12));
        botonMultiplicacion.setForeground(java.awt.Color.red);
        botonMultiplicacion.setText("*");
        botonMultiplicacion.setMargin(new java.awt.Insets(2, 2, 2, 2));
        botonMultiplicacion.setName("3"); // NOI18N
        botonMultiplicacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                operacion(evt);
            }
        });

        botonDivision.setFont(new java.awt.Font("Arial", 0, 12));
        botonDivision.setForeground(java.awt.Color.red);
        botonDivision.setText("/");
        botonDivision.setMargin(new java.awt.Insets(2, 2, 2, 2));
        botonDivision.setName("4"); // NOI18N
        botonDivision.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                operacion(evt);
            }
        });

        botonIgual.setFont(new java.awt.Font("Arial", 0, 12));
        botonIgual.setText("=");

        botonBorrarDigito.setFont(new java.awt.Font("Arial", 0, 12));
        botonBorrarDigito.setForeground(java.awt.Color.blue);
        botonBorrarDigito.setText("←");
        botonBorrarDigito.setMargin(new java.awt.Insets(2, 2, 2, 2));
        botonBorrarDigito.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                borrarDigito(evt);
            }
        });

        botonBorrarTodo.setFont(new java.awt.Font("Arial", 0, 12));
        botonBorrarTodo.setText("C");

        botonSigno.setText("+/-");
        botonSigno.setMargin(new java.awt.Insets(2, 2, 2, 2));
        botonSigno.setName("+"); // NOI18N
        botonSigno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonSignoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout calculadoraLayout = new javax.swing.GroupLayout(calculadora);
        calculadora.setLayout(calculadoraLayout);
        calculadoraLayout.setHorizontalGroup(
            calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, calculadoraLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, calculadoraLayout.createSequentialGroup()
                        .addComponent(boton4, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(boton5, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(boton6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonMultiplicacion))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, calculadoraLayout.createSequentialGroup()
                        .addComponent(boton1, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(boton2, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(boton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonResta))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, calculadoraLayout.createSequentialGroup()
                        .addGroup(calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(argumento, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, calculadoraLayout.createSequentialGroup()
                                .addComponent(boton7, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(boton8, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(boton9, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(botonBorrarDigito, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(botonDivision, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(calculadoraLayout.createSequentialGroup()
                        .addGroup(calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, calculadoraLayout.createSequentialGroup()
                                .addComponent(botonBorrarTodo, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(botonIgual, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE))
                            .addGroup(calculadoraLayout.createSequentialGroup()
                                .addComponent(boton0, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(botonSigno)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(botonPunto, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(botonSuma, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addContainerGap(11, Short.MAX_VALUE))
        );

        calculadoraLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {boton0, boton1, boton2, boton3, boton4, boton5, boton6, boton7, boton8, boton9, botonBorrarDigito, botonDivision, botonMultiplicacion, botonPunto, botonResta, botonSigno, botonSuma});

        calculadoraLayout.setVerticalGroup(
            calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(calculadoraLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(argumento, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botonBorrarDigito))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(boton7, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(boton8)
                    .addComponent(boton9)
                    .addComponent(botonDivision))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(boton4)
                    .addComponent(boton5)
                    .addComponent(boton6)
                    .addComponent(botonMultiplicacion))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(boton1)
                    .addComponent(boton2)
                    .addComponent(boton3)
                    .addComponent(botonResta))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(boton0)
                    .addGroup(calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(botonSigno)
                        .addComponent(botonPunto)
                        .addComponent(botonSuma)))
                .addGap(6, 6, 6)
                .addGroup(calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonBorrarTodo)
                    .addComponent(botonIgual))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        calculadoraLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {boton0, boton1, boton2, boton3, boton4, boton5, boton6, boton7, boton8, boton9, botonDivision, botonMultiplicacion, botonPunto, botonResta, botonSigno, botonSuma});

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(calculadora, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(calculadora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void agregarDigito(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_agregarDigito
        JButton boton = (JButton) evt.getSource();
        argumento.setText(argumento.getText() + boton.getName());
}//GEN-LAST:event_agregarDigito

    private void borrarDigito(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_borrarDigito
        String original = argumento.getText();
        if (!original.equals("")) {
            argumento.setText(original.substring(0, original.length() - 1));
        }
}//GEN-LAST:event_borrarDigito

    private void botonSignoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonSignoActionPerformed
        JButton boton = (JButton) evt.getSource();
        if (boton.getName().equals("+")) {
            argumento.setText("-" + argumento.getText());
            boton.setName("-");
        } else {
            String original = argumento.getText();
            argumento.setText(original.substring(1, original.length()));
            boton.setName("+");
        }
}//GEN-LAST:event_botonSignoActionPerformed

    private void operacion(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_operacion
        JButton boton = (JButton) evt.getSource();
        int tipoDeOperacion = Integer.getInteger(boton.getText());
        System.out.println(boton.getName());
        List<Object> intr = new ArrayList<Object>();
        intr.add(5);
        intr.add(4);
        System.out.println(Controller.execute(tipoDeOperacion, tipoDeDatos, intr));
}//GEN-LAST:event_operacion

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new GUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField argumento;
    private javax.swing.JButton boton0;
    private javax.swing.JButton boton1;
    private javax.swing.JButton boton2;
    private javax.swing.JButton boton3;
    private javax.swing.JButton boton4;
    private javax.swing.JButton boton5;
    private javax.swing.JButton boton6;
    private javax.swing.JButton boton7;
    private javax.swing.JButton boton8;
    private javax.swing.JButton boton9;
    private javax.swing.JButton botonBorrarDigito;
    private javax.swing.JButton botonBorrarTodo;
    private javax.swing.JButton botonDivision;
    private javax.swing.JButton botonIgual;
    private javax.swing.JButton botonMultiplicacion;
    private javax.swing.JButton botonPunto;
    private javax.swing.JButton botonResta;
    private javax.swing.JButton botonSigno;
    private javax.swing.JButton botonSuma;
    private javax.swing.JPanel calculadora;
    // End of variables declaration//GEN-END:variables
}
